//
//  FailureLayer.cpp
//  firebkball
//
//  Created by ren jian on 18/5/29.
//
//

#include "FailureLayer.h"
#include "WelcomeLayer.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "stdtostring.h"
#endif

#include "SimpleAudioEngine.h"

bool FailureLayer::init()
{
    if (!Layer::init()) {
        return false;
    }
    m_lastTime = 5;
    
    m_size = Director::getInstance()->getWinSize();
    this->ignoreAnchorPointForPosition(false);
    this->setAnchorPoint(Vec2(0.5, 0.5));
    this->setContentSize(m_size);
    this->setPosition(m_size/2);
    
    
    m_controller = CSLoader::createNode("res/failureView.csb");
    m_controller->setAnchorPoint(Vec2(0.5, 0.5));
    m_controller->setPosition(m_size/2);
    this->addChild(m_controller);
    
    // 倒计时
    Node *l_node = m_controller->getChildByName("l_box")->getChildByName("nd_time");
    m_timeLab = Label::createWithTTF(to_string(m_lastTime-1), "font/002.ttf", 100);
    m_timeLab->setTextColor(Color4B(82, 149, 20, 255));
    l_node->addChild(m_timeLab);
    
    m_fireballTimer = ProgressTimer::create(Sprite::create("imgs/balltimer.png"));
    m_fireballTimer->setAnchorPoint(Vec2(0.5, 0.5));
    m_fireballTimer->setPosition(0, 0);
    m_fireballTimer->setType(ProgressTimer::Type::RADIAL);
    m_fireballTimer->setReverseDirection(true);
    l_node->addChild(m_fireballTimer, 9);
    m_fireballTimer->setPercentage(100);
    
    cocos2d::ui::Text *l_scoreLab = (cocos2d::ui::Text*)m_controller->getChildByName("l_box")->getChildByName("lab_score");
    l_scoreLab->setString(to_string(UserDataManager::getInstance()->battleData->score));
    return true;
}

void FailureLayer::onEnter()
{
    Layer::onEnter();
    
    runProgAction();
    CCLOG("游戏失败: ");
    
    CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("audio/failure.mp3");
    
    this->addCoin(0);
}

void FailureLayer::runProgAction()
{
    m_fireballTimer->runAction(Repeat::create(Sequence::create(
                                                               CallFunc::create([=](){
        CCLOG("step 1");
        m_fireballTimer->setPercentage(100);
        m_lastTime--;
        m_timeLab->setString(to_string(m_lastTime));
        m_timeLab->setScale(0.9);
        m_timeLab->runAction(EaseBackOut::create(ScaleTo::create(0.2, 1)));
        
    }),
                                                               ProgressTo::create(1, 0),
                                                               CallFunc::create([=](){
        CCLOG("step 2");
        if (m_lastTime==0) {
            // TODO::超时
            onGameEnd();
        }
    })
                                                               , NULL), 10));
}

void FailureLayer::addCoin(int _number)
{
    UserDataManager::getInstance()->addCoin(_number);
    this->changeCoin();
    CCLOG("投币数: %d", UserDataManager::getInstance()->userData->coin);
}

void FailureLayer::changeCoin()
{
    auto lab_coinNumber = (cocos2d::ui::Text*)m_controller->getChildByName("l_box")->getChildByName("lab_coin");
    string l_inCoin = StringUtils::format("%d / %d", UserDataManager::getInstance()->userData->coin, UserDataManager::getInstance()->userData->startCoinNumber);
    lab_coinNumber->setString(l_inCoin);
}

void FailureLayer::onGameEnd()
{
    // 保存分数top
    UserDataManager::getInstance()->userData->refreshScoreTop(UserDataManager::getInstance()->battleData->score);
    // 重置数据
    UserDataManager::getInstance()->battleData->resethData();
    
    Director::getInstance()->replaceScene(WelcomeLayer::createScene());
}

void FailureLayer::onExit()
{
    Layer::onExit();
}










